﻿using System.Linq;
using System.Web.Mvc;
using OurWedding1.Models;

namespace OurWedding1.Controllers
{
    public class BaseController : Controller
    {
        public BaseController()
        {
            UsersInWeddingRepository = new UsersInWeddingRepository();
        }
        protected UsersInWeddingRepository UsersInWeddingRepository;
        protected int? SelectedWeddingId
        {
            get
            {
                if (Session["_WeddingId"] != null)
                {
                    return (int)Session["_WeddingId"];
                }

                var context = new UsersContext();
                var username = User.Identity.Name;
                var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
                if (user != null)
                {
                    var userId = user.UserId;
                    var uiw = UsersInWeddingRepository.All.SingleOrDefault(u => u.UserId == userId); //TODO:Improve
                    if (uiw != null)
                    {
                        Session.Add("_WeddingId", uiw.WeddingId);
                        return uiw.WeddingId;

                    }
                }

                return null;
            }

            set
            {
                Session["_WeddingId"] = value;
            }
        }
    }
}
